<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Match_entity{
	var $id = 0;
	var $home = 0;
	var $home_score = '';
	var $home_wickets = '';
	var $home_overs = '';
	//var $home_runs = 0;
	var $away = 0;
	var $away_score = '';
	var $away_wickets = '';
	var $away_overs = '';
	//var $away_runs = 0;
	var $start_time = 0;
	var $end_time = 0;
	var $status = "";
	var $bet_enable = "";
	var $tournament_id = 0;
	var $sport_id = 0;
	var $convertion_rate = "";
	var $enable_convertion = "yes";
	var $result = "";
	var $email_notify_enable = "";
	var $sms_notify_enable = "";
	var $notify_result = "";
	var $feed_type = "";
	var $feed_key = "";
	var $feed_match_id = "";
	var $feed_category_id = "";
	var $batman_info = "";
	var $title = "";
	var $show_feature = 0;
	var $feed_search = '';
	var $batting_team = '';
	var $playing = '';
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_MATCH)->result();
		}
		else if($cmd == 'feed_match_id'){
			$result = $ci->db->where('feed_match_id',$input)->get(TBL_MATCH)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->home = $result[0]->home;
			$this->home_score = $result[0]->home_score;
			$this->home_wickets = $result[0]->home_wickets;
			$this->home_overs = $result[0]->home_overs;
			//$this->home_runs = $result[0]->home_runs;
			$this->away = $result[0]->away;
			$this->away_score = $result[0]->away_score;
			$this->away_wickets = $result[0]->away_wickets;
			$this->away_overs = $result[0]->away_overs;
			//$this->away_runs = $result[0]->away_runs;
			$this->start_time = $result[0]->start_time;
			$this->end_time = $result[0]->end_time;
			$this->status = $result[0]->status;
			$this->bet_enable = $result[0]->bet_enable;
			$this->tournament_id = $result[0]->tournament_id;
			$this->sport_id = $result[0]->sport_id;
			$this->convertion_rate = $result[0]->convertion_rate;
			$this->enable_convertion = $result[0]->enable_convertion;
			$this->result = $result[0]->result;
			$this->email_notify_enable = $result[0]->email_notify_enable;
			$this->sms_notify_enable = $result[0]->sms_notify_enable;
			$this->notify_result = $result[0]->notify_result;
			$this->feed_type = $result[0]->feed_type;
			$this->feed_key = $result[0]->feed_key;
			$this->feed_match_id = $result[0]->feed_match_id;
			$this->feed_category_id = $result[0]->feed_category_id;
			$this->batman_info = $result[0]->batman_info;
			$this->title = $result[0]->title;
			$this->show_feature = $result[0]->show_feature;
			$this->feed_search = $result[0]->feed_search;
			$this->batting_team = $result[0]->batting_team;
			$this->playing = $result[0]->playing;
			
			return $this;
		}
		return false;
	}
	
	function setHome($input){ $this->home=$input; }
	function setHomeScore($input){ $this->home_score=$input;}
	function setHomeWickets($input){ $this->home_wickets=$input;}
	function setHomeOvers($input){ $this->home_overs=$input;}
	//function setHomeRuns($input){ $this->home_runs=$input;}
	function setAway($input){ $this->away=$input; }
	function setAwayScore($input){ $this->away_score=$input;}
	function setAwayWickets($input){ $this->away_wickets=$input;}
	function setAwayOvers($input){ $this->away_overs=$input;}
	//function setAwayRuns($input){ $this->away_runs=$input;}
	function setStartTime($input){ $this->start_time=$input; }
	function setEndTime($input){ $this->end_time = $input; }
	function setBetEnable($input){ $this->bet_enable = $input; }
	function setStatus($input){ $this->status=$input; }
	function setTournamentId($input){ $this->tournament_id = $input; }
	function setSportId($input){ $this->sport_id = $input; }
	function setConvertionRate($input){ $this->convertion_rate = $input; }
	function setEnableConvertion($input){ $this->enable_convertion = $input; }
	function setResult($input){ $this->result = $input; }
	function setEmailNotifyEnable($input){ $this->email_notify_enable = $input; }
	function setSmsNotifyEnable($input){ $this->sms_notify_enable = $input; }
	function setNotifyResult($input){ $this->notify_result = $input; }
	function setFeedType($input){ $this->feed_type = $input; }
	function setFeedKey($input){ $this->feed_key = $input; }
	function setFeedMatchId($input){ $this->feed_match_id = $input; }
	function setFeedCategoryId($input){ $this->feed_category_id=$input; }
	function setBatmanInfo($input){ $this->batman_info=$input; }
	function setTitle($input){ $this->title=$input; }
	function setShowFeature($input){ $this->show_feature=$input; }
	function setFeedSearch($input){ $this->feed_search=$input;}
	function setBattingTeam($input){ $this->batting_team=$input; }
	function setPlaying($input){ $this->playing=$input; }
	
	function getId(){ return $this->id; }
	function getHome(){ return $this->home; }
	function getHomeScore(){ return $this->home_score;}
	function getHomeWickets(){ return $this->home_wickets;}
	function getHomeOvers(){ return $this->home_overs;}
	//function getHomeRuns(){ return $this->home_runs;}	
	function getAway(){ return $this->away;}
	function getAwayScore(){ return $this->away_score;}
	function getAwayWickets(){ return $this->away_wickets;}
	function getAwayOvers(){ return $this->away_overs;}	
	//function getAwayRuns(){ return $this->away_runs;}	
	function getStartTime(){ return $this->start_time;}
	function getEndTime(){ return $this->end_time;}
	function getBetEnable(){ return $this->bet_enable;}
	function getStatus(){ return $this->status;}
	function getTournamentId(){ return $this->tournament_id; }
	function getSportId(){ return $this->sport_id; }
	function getConvertionRate(){ return $this->convertion_rate; }
	function getEnableConvertion(){ return $this->enable_convertion; }
	function getResult(){ return $this->result; }
	function getEmailNotifyEnable(){ return $this->email_notify_enable; }
	function getSmsNotifyEnable(){ return $this->sms_notify_enable; }
	function getNotifyResult(){ return $this->notify_result;}
	function getFeedType(){ return $this->feed_type; }
	function getFeedKey(){ return $this->feed_key; }
	function getFeedMatchId(){ return $this->feed_match_id; }
	function getFeedCategoryId(){ return $this->feed_category_id; }
	function getBatmanInfo(){ return $this->batman_info; }
	function getTitle(){ return $this->title; }
	function getShowFeature(){ return $this->show_feature; }
	function getFeedSearch(){ return $this->feed_search; }
	function getBattingTeam(){ return $this->batting_team; }
	function getPlaying(){ return $this->playing; }
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('home', $this->home);
		$ci->db->set('home_score', $this->home_score);
		$ci->db->set('home_wickets', $this->home_wickets);
		$ci->db->set('home_overs', $this->home_overs);
		//$ci->db->set('home_runs', $this->home_runs);
		$ci->db->set('away', $this->away);
		$ci->db->set('away_score', $this->away_score);
		$ci->db->set('away_wickets', $this->away_wickets);
		$ci->db->set('away_overs', $this->away_overs);
		//$ci->db->set('away_runs', $this->away_runs);
		$ci->db->set('start_time', $this->start_time);
		$ci->db->set('end_time', $this->end_time);
		$ci->db->set('bet_enable', $this->bet_enable);
		$ci->db->set('status', $this->status);
		$ci->db->set('tournament_id', $this->tournament_id);
		$ci->db->set('sport_id', $this->sport_id);
		$ci->db->set('convertion_rate', $this->convertion_rate);
		$ci->db->set('enable_convertion', $this->enable_convertion);
		$ci->db->set('result', $this->result);
		$ci->db->set('email_notify_enable', $this->email_notify_enable);
		$ci->db->set('sms_notify_enable', $this->sms_notify_enable);
		$ci->db->set('notify_result', $this->notify_result);
		$ci->db->set('feed_type', $this->feed_type);
		$ci->db->set('feed_key', $this->feed_key);
		$ci->db->set('feed_match_id', $this->feed_match_id);
		$ci->db->set('feed_category_id', $this->feed_category_id);
		$ci->db->set('batman_info', $this->batman_info);
		$ci->db->set('title', $this->title);
		$ci->db->set('show_feature', $this->show_feature);
		$ci->db->set('feed_search', $this->feed_search);
		$ci->db->set('batting_team', $this->batting_team);
		$ci->db->set('playing', $this->playing);
		
		$ci->db->where('id', $this->id)->update(TBL_MATCH);
	}
	
	function createNewMatchEntity(){
		$ci=& get_instance();
		$ci->db->set('bet_enable', 'yes');
		$ci->db->insert(TBL_MATCH);
		
		return $ci->db->insert_id();
	}
	
	function isMatchFinished(){
		$ci=& get_instance();
		$isFinish = true;
		$betArray = $ci->bet_entity->getBetListOfMatch($this->id);
		foreach($betArray as $obj){
			if($obj->result == 'pending' OR $obj->status == 'betting'){
				$isFinish = false;
				break;
			}
		}
		$eventArray = $ci->event_entity->getLivingEventOfMatch($this->id);
		if(!empty($eventArray)){
			$isFinish = false;
		}
		return $isFinish;
	}
	
	function getListMatchLiveNext5Mins(){
		$ci = & get_instance();
		$timeCond = time() + 300;
		$matches = $ci->db->where("start_time <= ".$timeCond." AND notify_result IS NULL AND status LIKE 'upcoming'")->get(TBL_MATCH)->result();
		foreach($matches as $item){
			$ci->db->set('notify_result', 'sent');
			$ci->db->where('id', $item->id)->update(TBL_MATCH);
		}
		return $matches;
	}
	
	function getArrayListOfMatch($tournament_id){
		$ci=& get_instance();
		$array = array();
		$array[] = $this->getMatchOfTournamentByStatus($tournament_id,'living');
		$array[] = $this->getMatchOfTournamentByStatus($tournament_id,'upcoming');
		$array[] = $this->getMatchOfTournamentByStatus($tournament_id,'closed');
		$array[] = $this->getMatchOfTournamentByStatus($tournament_id,'cancelled');
		
		$tmp = array();
		foreach($array as $k=>$v){
			foreach($v as $arr){
				$tmp[] = $arr;
			}
		}
		return $tmp;//$ci->db->where('tournament_id', $tournament_id)->order_by('start_time','desc')->get(TBL_MATCH)->result();
	}
	
	function getMatchOfTournamentByStatus($tournament_id, $status="living"){
		$ci=& get_instance();
		if($status == "living"){
			return $ci->db->where("status LIKE 'living' AND tournament_id='$tournament_id'")
					->order_by('id','asc')->get(TBL_MATCH)->result();
		}	
		if($status == "upcoming"){
			return $ci->db->where("status LIKE 'upcoming' AND tournament_id='$tournament_id'")
					->order_by('id','asc')->get(TBL_MATCH)->result();
		}
		if($status == "closed"){
			return $ci->db->where("status LIKE 'closed' AND tournament_id='$tournament_id'")
					->order_by('id','asc')->get(TBL_MATCH)->result();
		}
		if($status == "cancelled"){
			return $ci->db->where("status LIKE 'cancelled' AND tournament_id='$tournament_id'")
					->order_by('id','asc')->get(TBL_MATCH)->result();
		}
	}
	
	function changeStatusMatchRealTime(){
		$ci=& get_instance();
		$ci->db->where("status LIKE 'upcoming' AND start_time <=".time())->update(TBL_MATCH, array('status'=>'living'));
		$ci->db->where("status LIKE 'living' AND end_time <=".time())->update(TBL_MATCH, array('status'=>'closed'));		
	}
	
	function getMyLiveMatch($user_id){
		$ci=& get_instance();
		
		$matchIdArray = array();
		$tournamentIdArray = array();
		
		foreach( $ci->bet_entity->getMyBetMatch($user_id) as $item){
			$matchIdArray[] = $item->match_id;
		}
		$matchIdArray = array_unique($matchIdArray);
		if(empty($matchIdArray)){return null;}
		
		$array_return = array();
		$array_tmp = array();
		foreach($matchIdArray as $id){
			$tmp = $ci->db->where("(status LIKE 'living' OR status LIKE 'upcoming') AND id=$id")->get(TBL_MATCH)->result();
			if(!empty($tmp)){
				$array_tmp[] = $tmp[0];
				$tournamentIdArray[] = $tmp[0]->tournament_id;
			}		
		}		
		foreach($tournamentIdArray as $t_id){
			$array_return[$t_id] = array();
			foreach($array_tmp as $item){
				if($item->tournament_id == $t_id)
					$array_return[$t_id][] = $item->id;
			}
		}	
		return $array_return;
	}
	
	function getMyClosedMatch($user_id){
		$ci=& get_instance();
		
		$matchIdArray = array();
		$tournamentIdArray = array();
		
		foreach( $ci->bet_entity->getMyBetMatch($user_id) as $item){
			$matchIdArray[] = $item->match_id;
		}
		$matchIdArray = array_unique($matchIdArray);
		if(empty($matchIdArray)){return null;}
		
		$array_return = array();
		$array_tmp = array();
		foreach($matchIdArray as $id){
			$tmp = $ci->db->where("(status LIKE 'closed') AND id=$id")->get(TBL_MATCH)->result(); //OR status LIKE 'cancelled'
			if(!empty($tmp)){
				$array_tmp[] = $tmp[0];
				$tournamentIdArray[] = $tmp[0]->tournament_id;
			}		
		}		
		foreach($tournamentIdArray as $t_id){
			$array_return[$t_id] = array();
			foreach($array_tmp as $item){
				if($item->tournament_id == $t_id)
					$array_return[$t_id][] = $item->id;
			}
		}	
		return $array_return;
	}
	
	function displayMatchInfo($team){
		$ci=& get_instance();
		$over = ' Ov';
		if($this->sport_id == 1){
			$over = '';
		}
		
		if($team == 'home'){
			$TEAM = clone_( $ci->team_entity->init('id', $this->getHome()) );
			$name = $TEAM->getCode() ? $TEAM->getCode() : $TEAM->getName();
			$score = strlen($this->getHomeScore()) ? ' '.$this->getHomeScore() : '';
			$wk	   = strlen($score) ? ( strlen($this->getHomeWickets()) ? '-'.$this->getHomeWickets() : '-0' ) : '';
			$ov	   = strlen($this->getHomeOvers()) ? ' ('.$this->getHomeOvers()." $over)" : '';
			
			return '<span class="code">'.$name.'</span>'.$score.$wk.$ov;
		}
		else if($team == 'away'){
			$TEAM = clone_( $ci->team_entity->init('id', $this->getAway()) );
			$name = $TEAM->getCode() ? $TEAM->getCode() : $TEAM->getName();
			$score = strlen($this->getAwayScore()) ? ' '.$this->getAwayScore() : '';
			$wk	   = strlen($score) ? ( strlen($this->getAwayWickets()) ? '-'.$this->getAwayWickets() : '-0' ) : '';
			$ov	   = strlen($this->getAwayOvers()) ? ' ('.$this->getAwayOvers()." $over)" : '';
			
			return '<span class="code">'.$name.'</span>'.$score.$wk.$ov;
		}
		else if($team == 'closed'){
			/*  $team_id = $ci->event_entity->getWonTeamOfMatch($this->getId());
			if($team_id == 0){return "";}
			$TEAM = $ci->team_entity->init('id', $team_id);
			$name = $TEAM->getCode() ? $TEAM->getCode() : $TEAM->getName();
			$runs = ( $this->getHome() == $team_id ) ? $this->getHomeRuns() : $this->getAwayRuns();
			return $name." Won by ".$runs." Runs"; */
			return strlen($this->getResult()) ? $this->getResult(): '';
		}
		else{
			return '';
		}
	}
	
	function getAllMatches(){
		$ci=& get_instance();
		return $ci->db->get(TBL_MATCH)->result();
	}
	
	//end class
}
